package com.shujia.MR.partitions;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Unable to initialize MapOutputCollector org.apache.hadoop.mapred.MapTask$MapOutputBuffer
 * java.lang.NullPointerException
 *
 * 遇到该错误说明当前Map端输出类有问题，通常是由于输出类没有序列化导致
 */
public class Student implements Writable {
    //1500100001,施笑槐,22,女,文科六班
    String studentID;
    String name;
    String age;
    String gender;
    String clazz;

    @Override
    public String toString() {
        return "Student{" +
                "studentID='" + studentID + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", gender='" + gender + '\'' +
                ", clazz='" + clazz + '\'' +
                '}';
    }

    public Student(String studentID, String name, String age, String gender, String clazz) {
        this.studentID = studentID;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.clazz = clazz;
    }

    public Student() {
    }

    public String getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    /**
     * 序列化过程，将当前属性，转为二进制
     * @param out
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        // UTF表示对应字符串
        out.writeUTF(this.studentID);
        out.writeUTF(this.name);
        out.writeUTF(this.age);
        out.writeUTF(this.gender);
        out.writeUTF(this.clazz);
    }

    /**
     * 反序列化过程读取对应属性并赋予给新的对象
     * @param in
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.studentID = in.readUTF();
        this.name = in.readUTF();
        this.age = in.readUTF();
        this.gender = in.readUTF();
        this.clazz = in.readUTF();
    }
}
